package com.cgsd.heyu.adapter;

import android.content.Context;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;

import com.android.library.adapter.BaseMultiItemAdapter;
import com.android.library.adapter.ViewHolder;
import com.cgsd.heyu.R;
import com.cgsd.heyu.entity.DefaultAvatarEntity;

import java.util.List;

/**
 * @author xiaoxing
 * Description 头像选择
 * CreateData: 2020/11/2
 */
public class AvatarSelectAdapter extends BaseMultiItemAdapter<DefaultAvatarEntity> {
    private int selectPosition;

    public int getSelectPosition() {
        return selectPosition;
    }

    public void setSelectPosition(int selectPosition) {
        if (this.selectPosition == selectPosition){
            return;
        }
        int temp = this.selectPosition;
        this.selectPosition = selectPosition;
        notifyItemChanged(temp,"payload");
        notifyItemChanged(selectPosition,"payload");
    }

    public AvatarSelectAdapter(Context mContext, List<DefaultAvatarEntity> mDatas) {
        super(mContext, mDatas);
    }

    @Override
    protected int muItemViewType(int position, DefaultAvatarEntity tabEntity) {
        return 0;
    }

    @Override
    protected int muLayoutId(int itemType) {
        return R.layout.item_avatar_select;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull List<Object> payloads) {
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            RelativeLayout layoutAvatar = holder.getView(R.id.layout_avatar);
            if (selectPosition == position){
                layoutAvatar.setBackgroundResource(R.drawable.border_mid_blue);
            }else {
                layoutAvatar.setBackground(null);
            }
        }
    }

    @Override
    public void convert(ViewHolder holder, DefaultAvatarEntity tabEntity, int position) {
        RelativeLayout layoutAvatar = holder.getView(R.id.layout_avatar);
        if (selectPosition == position){
            layoutAvatar.setBackgroundResource(R.drawable.border_mid_blue);
        }else {
            layoutAvatar.setBackground(null);
        }
        holder.setImageUrl(R.id.avatar,tabEntity.defaultHeadUrl,R.mipmap.default_avatar_min);
    }
}
